//
//  CommonFunc.h
//  Pods
//
//  Created by Huadong Zheng on 2023/4/11.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "NSString+Custom.h"

#ifndef CommonFunc_h
#define CommonFunc_h


static inline NSMutableAttributedString* MASTR(NSArray<NSString *> * strings, NSArray * colors, NSArray * fonts){
    return [NSString stringWithArray:strings color:colors font:fonts];
}

static inline NSMutableAttributedString* MASTR_EXPAND(NSArray<NSString *> * strings, NSArray * colors, NSArray * fonts, NSDictionary<NSNumber *, NSDictionary *> * expandDicts){
    return [NSString stringWithArray:strings color:colors font:fonts expand:expandDicts];
}

static inline id bundleByClass(Class cls){
    NSBundle *bundle = [NSBundle bundleForClass:cls];
    NSString * bundleName = bundle.infoDictionary[(NSString *)kCFBundleNameKey];
    NSURL *url = [[bundle resourceURL] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.bundle",bundleName]];
    return [NSBundle bundleWithURL:url];
}
static inline id bundleByName(NSString * bundleName){
    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *url = [[bundle resourceURL] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.bundle",bundleName]];
    return [NSBundle bundleWithURL:url];
}

static inline id imageByNameAndBundleClass(NSString * name, Class cls){
    
    NSBundle * bundle = bundleByClass(cls);
    UIImage * image = [UIImage imageNamed:name inBundle:bundle compatibleWithTraitCollection:nil];
    return image;
}

static inline id nibViewByNameAndBundleClass(NSString * name, Class cls){
    NSBundle * bundle = bundleByClass(cls);
    UINib * nibView = [UINib nibWithNibName:name bundle:bundle];
    UIView * view = [nibView instantiateWithOwner:nil options:nil].firstObject;
    view.autoresizingMask = UIViewAutoresizingNone;
    return view;
}

static inline id nibViewByNameAndBundleName(NSString * name, NSString * bundleName){
    NSBundle * bundle = bundleByName(bundleName);
    UINib * nibView = [UINib nibWithNibName:name bundle:bundle];
    UIView * view = [nibView instantiateWithOwner:nil options:nil].firstObject;
    view.autoresizingMask = UIViewAutoresizingNone;
    return view;
}

static inline id nibClassByNameAndBundleClass(NSString * name, Class cls){
    NSBundle * bundle = bundleByClass(cls);
    UINib * nib = [UINib nibWithNibName:name bundle:bundle];
    return nib;
}


#endif
